package com.zxyankh.generator.math.practice;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.zxyankh.generator.math.practice.dto.Options;
import com.zxyankh.generator.math.practice.dto.Problem;

public class Generator {

	public List<Problem> generateProblems(Options options) {
		List<Problem> problemList = new ArrayList<>(options.getProblemNumber());
		Random random = new Random();
		int pre = 0, suf = 0;
		for (int i = 1; i <= options.getProblemNumber(); i++) {
			Problem problem = new Problem();
			problem.setNo(i);
			pre = random.nextInt(options.getSumCapped());
			suf = random.nextInt(options.getSumCapped() - pre);
			// 去掉因子为0的情况
			if (pre == 0 || suf == 0) {
				i--;
				continue;
			}
			if (options.isSubtraction()) {
				problem.setAdd(random.nextBoolean());
			}
			if (options.isNegativeNumber()) {
				pre *= (random.nextBoolean() ? 1 : -1);
				suf *= (random.nextBoolean() ? 1 : -1);
			}
			problem.setPreNumber(pre);
			problem.setSufNumber(suf);
			problem.calculate();
			if (!options.isNegativeNumber() && problem.getAnswer() < 0) {
				i--;
				continue;
			}
			problemList.add(problem);
		}
		return problemList;
	}

}
